import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 导入模块
import auth from './auth'
import packages from './packages'
import stats from './stats'
import tags from './tags'
import discussions from './discussions'
import sync from './sync'

const store = () => new Vuex.Store({
  modules: {
    auth,
    packages,
    stats,
    tags,
    discussions,
    sync
  },
  state: {
    loading: false,
    error: null
  },
  mutations: {
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_ERROR(state, error) {
      state.error = error
    }
  },
  actions: {
    setLoading({ commit }, loading) {
      commit('SET_LOADING', loading)
    },
    setError({ commit }, error) {
      commit('SET_ERROR', error)
    }
  },
  getters: {
    loading: state => state.loading,
    error: state => state.error
  }
})

export default store